package com.cwl.po.loop;

import org.springframework.beans.BeanUtils;

/**
 * @program: cwl-performance-optimization
 * @description: 测试for循环中的反射操作
 * @author: ChenWenLong
 * @create: 2019-11-22 14:00
 **/
public class TestForReflect extends Object{

    // 事实证明我们在日常开发中喜欢使用反射拷贝对象信息,以此起到节省代码量的操作
    // 但是在for循环中使用反射带来的巨大开销是远不如使用set方法一个个去设置值来的简单
    public static void main(String[] args) {
        System.out.println(testUseReflect());
        System.out.println(testNotUseReflect());
    }

    /**
     * 功能描述:
     * 〈测试不使用反射〉
     *
     * @params : []
     * @return : long
     * @author : cwl
     * @date : 2019/11/22 14:04
     */
    private static long testNotUseReflect() {
        long begin = System.currentTimeMillis();
        User user = new User(1,"陈文龙",18,"广东","1120","3845","测试");
        UserVO userVO;
        for(int i=0;i<10000;i++){
            userVO = new UserVO();
            userVO.setAdress(user.getAdress());
            userVO.setAge(user.getAge());
            userVO.setIDCard(user.getIDCard());
            userVO.setMsg(user.getMsg());
            userVO.setName(user.getName());
            userVO.setPhoneNumber(user.getPhoneNumber());
            userVO.setId(user.getId());
        }
        long end = System.currentTimeMillis();
        return end - begin;
    }

    /**
     * 功能描述:
     * 〈测试for循环中使用反射〉
     *
     * @params : []
     * @return : long
     * @author : cwl
     * @date : 2019/11/22 14:03
     */
    private static long testUseReflect() {
        long begin = System.currentTimeMillis();
        User user = new User(1,"陈文龙",18,"广东","1120","3845","测试");
        UserVO userVO= new UserVO();
        for(int i=0;i<10000;i++){
            BeanUtils.copyProperties(user,userVO);
        }
        long end = System.currentTimeMillis();
        return end - begin;
    }
}

class User{

    private long id;
    private String name;
    private int age;
    private String adress;
    private String phoneNumber;
    private String IDCard;
    private String msg;

    public User(long id, String name, int age, String adress, String phoneNumber, String IDCard, String msg) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.adress = adress;
        this.phoneNumber = phoneNumber;
        this.IDCard = IDCard;
        this.msg = msg;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getIDCard() {
        return IDCard;
    }

    public void setIDCard(String IDCard) {
        this.IDCard = IDCard;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

class UserVO{
    private long id;
    private String name;
    private int age;
    private String adress;
    private String phoneNumber;
    private String IDCard;
    private String msg;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getIDCard() {
        return IDCard;
    }

    public void setIDCard(String IDCard) {
        this.IDCard = IDCard;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
